const Koa = require('koa2'),
    Router = require('koa-router'),
    cheerio = require('cheerio'),
    superagent = require('superagent'),
    app = new Koa(),
    router = new Router();

let reptileList = []


router.get('/', async ctx => {
    let url = 'https://news.baidu.com/';

    superagent.get(url).end((err, res) => {
        let html = res.text,
            $ = cheerio.load(html, {
                decodeEntities: false
            })

        $("#pane-news ul li").each((index, item) => {
            let temp = {
                tit: $(item).text(),
                url: $(item).find('a').attr('href')
            }
            reptileList.push(temp)
        })
    })
    ctx.body = {
        reptileList,
        msg: '无数据刷新重试、爬取后添加到数据库或者fs到本地文件'
    }
})

app.use(router.routes()).use(router.allowedMethods())
app.listen(7000, () => {
    console.log('Crawling...')
})